exports.AvatarHandler = Core.Class.singleton(
{
    classname: 'AvatarHandler',
    
    initialize: function(mallUI) 
    {
    	this._avatarsUIArray = null;
		
    	this._avatarsUIArray = new GAMutableArray();
    },
    
    initializeDefaultAvatars: function()
    {
    	var avatarsToBeAdded = this.getNoOftAvatarsToAdd();
    	for(var i=0 ; i < avatarsToBeAdded; i++)
    	{
    		var floor = this.getRandomFloorForAvatar();
	    	var avatarUI = new AvatarUI();
			avatarUI.setCurrentFloor(floor);
			avatarUI.setPosition(this.getAvatarStartUpPositionForFloor(floor));
			avatarUI.createView();
			avatarUI.setDepth(99998);
			MMGameLayer.getMallUI().addChild(avatarUI);
			this._avatarsUIArray.addObject(avatarUI);
    	}
    	MMFriendActionManager.checkAndAddStolenCustomers();
    },
    initNewAvatarTimer: function()
    {
    	this._checkForAvatarTimer = setInterval(this.checkAndAddAvatar.bind(this), 30000);
    },    
    
    addRandomAvatar: function()
	{
		var avatarUI = new AvatarUI();
		var floor = 0;
		avatarUI.setCurrentFloor(floor);
		avatarUI.setPosition(TCUtil.getAdjustedX(20), TCUtil.getAdjustedY(-35));
		avatarUI.createView();
		avatarUI.setDepth(99998);
		MMGameLayer.getMallUI().addChild(avatarUI);
		this._avatarsUIArray.addObject(avatarUI);
		MMGameLayer.getControlLayer().getTopBar().updateTopBar();
	},
	
	checkAndAddAvatar: function()
	{
		var noOfAvatars = this.getNoOftAvatarsToAdd();
		var currentNoOfAvatars = this._avatarsUIArray.getLength();
		if(currentNoOfAvatars < noOfAvatars && currentNoOfAvatars < MAX_NO_OF_AVATARS)
		{
			this.addRandomAvatar();
		}
	},
	
	removeRandomAvatar: function()
	{
		
	},
	
	removeAvatar: function(amount)
	{
		if(this._avatarsUIArray)
		{
			var avatarCount = this._avatarsUIArray.getLength();
			for(var i = 0; i < amount; i++)
			{
				var avatar = this._avatarsUIArray.pop();
				avatar.destroy();
			}
		}
	},
	
	getNoOftAvatarsToAdd: function()
	{
		var noOfStores = MMGameLayer.getMallUI().getStoresUIArray().getLength();
		var noOfAvatarsToAdd = Math.ceil(ATATARS_PER_STORE * noOfStores);
		if(!DatabaseInterface._isVisitingNeighbor)
		{
			var stolenAvatars = MMFriendActionManager.getStolenCustomerCount();
			var finalAvatarCount = noOfAvatarsToAdd - stolenAvatars;
			if(noOfAvatarsToAdd > 5)
			{
				if(finalAvatarCount < 5)
				{
					finalAvatarCount = 5;
				}
				return finalAvatarCount;
			}
		}
		return noOfAvatarsToAdd;
	},
	
	getAvatarStartUpPositionForFloor: function(floor)
	{
    	var positionY = TCUtil.getAdjustedY(-(35 + (floor) * FLOOR_HEIGHT));
    	var positionX = Math.round(Math.random()*330);
    	var position = new Core.Vector(positionX, positionY);
    	return position;
	},
	
	getRandomFloorForAvatar: function()
	{
		var noOfFloors = MMGameLayer.getMallUI().getFloorsUIArray().getLength();
    	var randomFloorNumber = Math.round(Math.random()*(noOfFloors-1));	
    	return randomFloorNumber;	
	},
				
    getAvatarsUIArray: function()
    {
    	return this._avatarsUIArray;
    },
    
    clearAddAvatarTimer: function()
    {
    	if(this._checkForAvatarTimer)
    	{
    		clearTimeout(this._checkForAvatarTimer);
    		this._checkForAvatarTimer = null;
    	}
    },
    
    destroyAvatarUIObjects: function()
    {
    	var count = this._avatarsUIArray.getLength();
    	for(var i = 0; i < count;)
    	{
    		this._avatarsUIArray.objectAtIndex(i).destroy();
    		this._avatarsUIArray.removeObjectAtIndex(i);
    		count--;
    	}    	
    },
    
    getTotalNumberOfAvatars: function()
    {
    	return this._avatarsUIArray.getLength();
    },
	
	getBaseResourcePath: function()
	{
		return "./Content/images/avatarimages/";
	},
									   
	destroy: function()
    {
    	if(this._avatarsUIArray)
    	{
    		this._avatarsUIArray.destroyWithObjects();
    		this._avatarsUIArray = null;
    	}
    },
});
